package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.vo.request.ExperimentProcessSampleHstqSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessSampleHstqSearchVo;
import com.zhijian.medical.entity.vo.request.SampleHstqSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleHstqListVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleHstqVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSampleVo;
import com.zhijian.medical.service.ExperimentProcessSampleHstqService;
import com.zhijian.medical.service.ExperimentProcessSampleService;
import com.zhijian.medical.util.Page;
import gov.nist.isg.archiver.DirectoryArchiver;
import gov.nist.isg.archiver.FilesArchiver;
import gov.nist.isg.pyramidio.BufferedImageReader;
import gov.nist.isg.pyramidio.PartialImageReader;
import gov.nist.isg.pyramidio.ScalablePyramidBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * 样本核酸提取 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-08-19 15:49:01
 */
@Slf4j
@Controller
@Api(tags = "样本核酸提取")
public class SampleHstqController {

    @Autowired
    private ExperimentProcessSampleHstqService service;

    @Autowired
    private ExperimentProcessSampleService experimentProcessSampleService;

    /**
     * 实验里保存
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/sample/hstq/in/{id}")
    @ApiOperation("实验里保存")
    public boolean insert(@PathVariable("id") Long id, @RequestBody ExperimentProcessSampleHstqSaveOrUpdateVo param) {
        param.setId(id);
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.saveDetail(param);
    }

    /**
     * 添加实验
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/sample/hstq")
    @ApiOperation("添加实验")
    public boolean insert(@RequestBody SampleHstqSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.saveDetail(param);
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/sample/hstq/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody SampleHstqSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据RNA或者DNA查询要核酸提取的样本列表
     *
     * @param drType
     * @return
     */
    @Response
    @GetMapping("/sample/hstq/list/{drType}")
    @ApiOperation("根据RNA或者DNA查询要核酸提取的样本列表")
    public List<ExperimentProcessSampleVo> selectWaitHstq(@ApiParam(name = "drType", value = "RNA(0)，DNA(1)，FISH(2)，新冠病毒(3)")
                                                          @PathVariable("drType") Integer drType) {
        return experimentProcessSampleService.selectWaitHstqByType(drType);
    }

    /**
     * 进入实验的详情接口
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/sample/hstq/in/{id}")
    @ApiOperation("进入实验的详情接口")
    public ExperimentProcessSampleHstqVo selectVoById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 点击详情查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/sample/hstq/out/{id}")
    @ApiOperation("点击详情查询")
    public ExperimentProcessSampleHstqListVo selectListVoById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectListVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/sample/hstq/page")
    @ApiOperation("分页查询")
    public Page<ExperimentProcessSampleHstqListVo> selectByCondition(@RequestBody(required = false) ExperimentProcessSampleHstqSearchVo param) {
        if (param == null) {
            param = ExperimentProcessSampleHstqSearchVo.builder().build();
        }
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * csv文件导入
     *
     * @param id
     * @param file
     * @return
     */
    @Response
    @PostMapping("/sample/hstq/csv/import/{id}")
    @ApiOperation("csv文件导入")
    public List<ExperimentProcessSampleVo> importCsv(@ApiParam(name = "id", value = "核酸提取id") @RequestParam(value = "id") Long id, MultipartFile file) {
        return service.importCsv(id, file);
    }

    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
        File file = new File("D:\\data\\CMU");
        File file1 = new File("D:\\data\\ttt.tif");
        BufferedImage image = ImageIO.read(file1);
        boolean mkdirs = file.mkdirs();
        ScalablePyramidBuilder spb = new ScalablePyramidBuilder();
        FilesArchiver archiver = new DirectoryArchiver(file);
        PartialImageReader pir = new BufferedImageReader(image);
        spb.buildPyramid(pir, "test", archiver, 1);
        long endTime = System.currentTimeMillis();
        System.out.println("切割图片耗时：" + (endTime - startTime) / 1000 + "秒");

    }

}